/*******************************************************************************
 * Copyright (c) 2011 squid.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     squid - initial API and implementation
 ******************************************************************************/
package com.google.code.cubeirc.scripting;


import java.io.File;
import java.util.concurrent.ConcurrentSkipListSet;

import com.google.code.cubeirc.queue.MessageQueue;
import com.google.code.cubeirc.queue.MessageQueueEnum;

public final class ScriptManager {
	public static final ConcurrentSkipListSet<Script> scripts = new ConcurrentSkipListSet<Script>();

	public static void addScript(File f) {
		scripts.add(new Script(f));
	}

	public static void removeScript(File f) {
		for(Script s : scripts){
			if(s.getReference().equals(f)){
				scripts.remove(s);
				s.getFunctions().clear();
			}
		}
	}

	public static void modifyScript(File f) {
		for(Script s : scripts){
			if(s.getReference().equals(f)){
				s.reset();
			}
		}
		if (scripts.size() == 0)
			addScript(f);	
	}
	

	
	
	public static void ParseCmd(String line)
	{
		Boolean found = false;
		line = line.replace("\r\n", "").replace("/", "");
		String command = line.split(" ")[0];
		
		
		
		for(Script s : scripts){
			if(s.getFunctions().contains(command)){
				if(line.split(" ").length > 1){
					s.invoke(command, line.substring(line.indexOf(line.split(" ")[1])));
				} else {
					s.invoke(command, "");
				}
				found=true;
			}
		}
		
		if(!found){
			 MessageQueue.addQueue(MessageQueueEnum.CONSOLE, String.format("[color_red]%s is not valid alias. Please define it.!!", command));
		}
	}
		
	

	public static final String[] validExt = {".js",".rb",".py",".lua"};
	
	public static final String[] jsKeywords = {"break","case","catch","continue","default","do",
			"else","false","finally","for","if","new","null","return","switch",
			"true","try","while","function","var","gui","util","in","global","print","println","sound","importPackage","importClass"
	};
	
	public static final String[] rbKeywords = {"alias","and","BEGIN","break","case","class","def","defined",
		"do","else","elsif","END","end","ensure","false","for","if","in","module","next","nil","not","or",
		"redo","rescue","retry","return","self","super","then","true","undef","unless","until","when","while",
		"yield","require"
	};
	
	public static final String[] pyKeywords = {"and","as","assert","break","class","continue","def","del","elif",
		"else","except","exec","finally","for","from","global","if","import","in","is","lambda","not","or","pass",
		"print","raise","return","try","while","with","yield"
	};
	
	public static final String[] luaKeywords = {"break","do","end","else","elseif","function","if",
		"local","nil","not","or","repeat","return","then","until","while"
	};
}
